R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

#install.packages("arules")
library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
data<-read.transactions("C:\\Users\\hp\\Downloads\\Market_Basket_Optimisation.csv",sep=",",rm.duplicates = TRUE)
## distribution of transactions with duplicates:
## 1 
## 5
dim(data)
## [1] 7501  119
summary(data)
## transactions as itemMatrix in sparse format with
##  7501 rows (elements/itemsets/transactions) and
##  119 columns (items) and a density of 0.03288973 
## 
## most frequent items:
## mineral water          eggs     spaghetti  french fries     chocolate 
##          1788          1348          1306          1282          1229 
##       (Other) 
##         22405 
## 
## element (itemset/transaction) length distribution:
## sizes
##    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16 
## 1754 1358 1044  816  667  493  391  324  259  139  102   67   40   22   17    4 
##   18   19   20 
##    1    2    1 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   2.000   3.000   3.914   5.000  20.000 
## 
## includes extended item information - examples:
##              labels
## 1           almonds
## 2 antioxydant juice
## 3         asparagus
itemLabels(data)
##   [1] "almonds"              "antioxydant juice"    "asparagus"           
##   [4] "avocado"              "babies food"          "bacon"               
##   [7] "barbecue sauce"       "black tea"            "blueberries"         
##  [10] "body spray"           "bramble"              "brownies"            
##  [13] "bug spray"            "burger sauce"         "burgers"             
##  [16] "butter"               "cake"                 "candy bars"          
##  [19] "carrots"              "cauliflower"          "cereals"             
##  [22] "champagne"            "chicken"              "chili"               
##  [25] "chocolate"            "chocolate bread"      "chutney"             
##  [28] "cider"                "clothes accessories"  "cookies"             
##  [31] "cooking oil"          "corn"                 "cottage cheese"      
##  [34] "cream"                "dessert wine"         "eggplant"            
##  [37] "eggs"                 "energy bar"           "energy drink"        
##  [40] "escalope"             "extra dark chocolate" "flax seed"           
##  [43] "french fries"         "french wine"          "fresh bread"         
##  [46] "fresh tuna"           "fromage blanc"        "frozen smoothie"     
##  [49] "frozen vegetables"    "gluten free bar"      "grated cheese"       
##  [52] "green beans"          "green grapes"         "green tea"           
##  [55] "ground beef"          "gums"                 "ham"                 
##  [58] "hand protein bar"     "herb & pepper"        "honey"               
##  [61] "hot dogs"             "ketchup"              "light cream"         
##  [64] "light mayo"           "low fat yogurt"       "magazines"           
##  [67] "mashed potato"        "mayonnaise"           "meatballs"           
##  [70] "melons"               "milk"                 "mineral water"       
##  [73] "mint"                 "mint green tea"       "muffins"             
##  [76] "mushroom cream sauce" "napkins"              "nonfat milk"         
##  [79] "oatmeal"              "oil"                  "olive oil"           
##  [82] "pancakes"             "parmesan cheese"      "pasta"               
##  [85] "pepper"               "pet food"             "pickles"             
##  [88] "protein bar"          "red wine"             "rice"                
##  [91] "salad"                "salmon"               "salt"                
##  [94] "sandwich"             "shallot"              "shampoo"             
##  [97] "shrimp"               "soda"                 "soup"                
## [100] "spaghetti"            "sparkling water"      "spinach"             
## [103] "strawberries"         "strong cheese"        "tea"                 
## [106] "tomato juice"         "tomato sauce"         "tomatoes"            
## [109] "toothpaste"           "turkey"               "vegetables mix"      
## [112] "water spray"          "white wine"           "whole weat flour"    
## [115] "whole wheat pasta"    "whole wheat rice"     "yams"                
## [118] "yogurt cake"          "zucchini"
image(data[1:50])

itemFrequencyPlot(data,topN=20)

itemFrequencyPlot(data,topN=20,type="absolute",col="blue")

itemFrequencyPlot(data,topN=20,type="relative",col="red")

#install.packages("arulesViz")
library(arules)
library(arulesViz)
rules=apriori(data,parameter=list(support=0.004,confidence=0.5,maxlen=20))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5   0.004      1
##  maxlen target  ext
##      20  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 30 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[119 item(s), 7501 transaction(s)] done [0.00s].
## sorting and recoding items ... [114 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [35 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(rules)
##      lhs                     rhs                 support confidence    coverage     lift count
## [1]  {salmon,                                                                                 
##       spaghetti}          => {mineral water} 0.006799093  0.5049505 0.013464871 2.118363    51
## [2]  {olive oil,                                                                              
##       soup}               => {mineral water} 0.005199307  0.5820896 0.008932142 2.441976    39
## [3]  {pancakes,                                                                               
##       soup}               => {mineral water} 0.004266098  0.6274510 0.006799093 2.632276    32
## [4]  {frozen vegetables,                                                                      
##       soup}               => {mineral water} 0.005065991  0.6333333 0.007998933 2.656954    38
## [5]  {ground beef,                                                                            
##       soup}               => {mineral water} 0.005065991  0.5205479 0.009732036 2.183798    38
## [6]  {milk,                                                                                   
##       soup}               => {mineral water} 0.008532196  0.5614035 0.015197974 2.355194    64
## [7]  {chocolate,                                                                              
##       soup}               => {mineral water} 0.005599253  0.5526316 0.010131982 2.318395    42
## [8]  {eggs,                                                                                   
##       soup}               => {mineral water} 0.004932676  0.5441176 0.009065458 2.282677    37
## [9]  {soup,                                                                                   
##       spaghetti}          => {mineral water} 0.007465671  0.5233645 0.014264765 2.195614    56
## [10] {cooking oil,                                                                            
##       pancakes}           => {mineral water} 0.004666045  0.5932203 0.007865618 2.488672    35
## [11] {cooking oil,                                                                            
##       ground beef}        => {spaghetti}     0.004799360  0.5714286 0.008398880 3.281995    36
## [12] {cooking oil,                                                                            
##       eggs}               => {mineral water} 0.006399147  0.5454545 0.011731769 2.288286    48
## [13] {pancakes,                                                                               
##       whole wheat rice}   => {mineral water} 0.004132782  0.5961538 0.006932409 2.500979    31
## [14] {frozen vegetables,                                                                      
##       turkey}             => {mineral water} 0.004399413  0.5000000 0.008798827 2.097595    33
## [15] {milk,                                                                                   
##       turkey}             => {mineral water} 0.006132516  0.5411765 0.011331822 2.270338    46
## [16] {chicken,                                                                                
##       pancakes}           => {mineral water} 0.004799360  0.5294118 0.009065458 2.220983    36
## [17] {chicken,                                                                                
##       ground beef}        => {spaghetti}     0.004799360  0.5070423 0.009465405 2.912193    36
## [18] {chicken,                                                                                
##       chocolate}          => {mineral water} 0.007598987  0.5181818 0.014664711 2.173871    57
## [19] {ground beef,                                                                            
##       low fat yogurt}     => {mineral water} 0.004799360  0.5000000 0.009598720 2.097595    36
## [20] {olive oil,                                                                              
##       tomatoes}           => {spaghetti}     0.004399413  0.6111111 0.007199040 3.509912    33
## [21] {olive oil,                                                                              
##       tomatoes}           => {mineral water} 0.004132782  0.5740741 0.007199040 2.408350    31
## [22] {olive oil,                                                                              
##       shrimp}             => {mineral water} 0.004532729  0.5573770 0.008132249 2.338303    34
## [23] {frozen vegetables,                                                                      
##       olive oil}          => {spaghetti}     0.005732569  0.5058824 0.011331822 2.905531    43
## [24] {frozen vegetables,                                                                      
##       olive oil}          => {mineral water} 0.006532462  0.5764706 0.011331822 2.418404    49
## [25] {milk,                                                                                   
##       olive oil}          => {mineral water} 0.008532196  0.5000000 0.017064391 2.097595    64
## [26] {chocolate,                                                                              
##       olive oil}          => {mineral water} 0.008265565  0.5040650 0.016397814 2.114649    62
## [27] {ground beef,                                                                            
##       shrimp}             => {spaghetti}     0.005999200  0.5232558 0.011465138 3.005315    45
## [28] {ground beef,                                                                            
##       pancakes}           => {mineral water} 0.007465671  0.5137615 0.014531396 2.155327    56
## [29] {frozen vegetables,                                                                      
##       ground beef}        => {spaghetti}     0.008665511  0.5118110 0.016931076 2.939582    65
## [30] {frozen vegetables,                                                                      
##       ground beef}        => {mineral water} 0.009198773  0.5433071 0.016931076 2.279277    69
## [31] {ground beef,                                                                            
##       milk}               => {mineral water} 0.011065191  0.5030303 0.021997067 2.110308    83
## [32] {eggs,                                                                                   
##       ground beef}        => {mineral water} 0.010131982  0.5066667 0.019997334 2.125563    76
## [33] {frozen vegetables,                                                                      
##       ground beef,                                                                            
##       spaghetti}          => {mineral water} 0.004399413  0.5076923 0.008665511 2.129866    33
## [34] {frozen vegetables,                                                                      
##       milk,                                                                                   
##       spaghetti}          => {mineral water} 0.004532729  0.5483871 0.008265565 2.300588    34
## [35] {chocolate,                                                                              
##       frozen vegetables,                                                                      
##       spaghetti}          => {mineral water} 0.004132782  0.5254237 0.007865618 2.204252    31
inspect(sort(rules,by='lift')[1:20])
##      lhs                                     rhs             support    
## [1]  {olive oil, tomatoes}                => {spaghetti}     0.004399413
## [2]  {cooking oil, ground beef}           => {spaghetti}     0.004799360
## [3]  {ground beef, shrimp}                => {spaghetti}     0.005999200
## [4]  {frozen vegetables, ground beef}     => {spaghetti}     0.008665511
## [5]  {chicken, ground beef}               => {spaghetti}     0.004799360
## [6]  {frozen vegetables, olive oil}       => {spaghetti}     0.005732569
## [7]  {frozen vegetables, soup}            => {mineral water} 0.005065991
## [8]  {pancakes, soup}                     => {mineral water} 0.004266098
## [9]  {pancakes, whole wheat rice}         => {mineral water} 0.004132782
## [10] {cooking oil, pancakes}              => {mineral water} 0.004666045
## [11] {olive oil, soup}                    => {mineral water} 0.005199307
## [12] {frozen vegetables, olive oil}       => {mineral water} 0.006532462
## [13] {olive oil, tomatoes}                => {mineral water} 0.004132782
## [14] {milk, soup}                         => {mineral water} 0.008532196
## [15] {olive oil, shrimp}                  => {mineral water} 0.004532729
## [16] {chocolate, soup}                    => {mineral water} 0.005599253
## [17] {frozen vegetables, milk, spaghetti} => {mineral water} 0.004532729
## [18] {cooking oil, eggs}                  => {mineral water} 0.006399147
## [19] {eggs, soup}                         => {mineral water} 0.004932676
## [20] {frozen vegetables, ground beef}     => {mineral water} 0.009198773
##      confidence coverage    lift     count
## [1]  0.6111111  0.007199040 3.509912 33   
## [2]  0.5714286  0.008398880 3.281995 36   
## [3]  0.5232558  0.011465138 3.005315 45   
## [4]  0.5118110  0.016931076 2.939582 65   
## [5]  0.5070423  0.009465405 2.912193 36   
## [6]  0.5058824  0.011331822 2.905531 43   
## [7]  0.6333333  0.007998933 2.656954 38   
## [8]  0.6274510  0.006799093 2.632276 32   
## [9]  0.5961538  0.006932409 2.500979 31   
## [10] 0.5932203  0.007865618 2.488672 35   
## [11] 0.5820896  0.008932142 2.441976 39   
## [12] 0.5764706  0.011331822 2.418404 49   
## [13] 0.5740741  0.007199040 2.408350 31   
## [14] 0.5614035  0.015197974 2.355194 64   
## [15] 0.5573770  0.008132249 2.338303 34   
## [16] 0.5526316  0.010131982 2.318395 42   
## [17] 0.5483871  0.008265565 2.300588 34   
## [18] 0.5454545  0.011731769 2.288286 48   
## [19] 0.5441176  0.009065458 2.282677 37   
## [20] 0.5433071  0.016931076 2.279277 69
subrules<-rules[quality(rules)$confidence>0.4]
inspect(subrules)
##      lhs                     rhs                 support confidence    coverage     lift count
## [1]  {salmon,                                                                                 
##       spaghetti}          => {mineral water} 0.006799093  0.5049505 0.013464871 2.118363    51
## [2]  {olive oil,                                                                              
##       soup}               => {mineral water} 0.005199307  0.5820896 0.008932142 2.441976    39
## [3]  {pancakes,                                                                               
##       soup}               => {mineral water} 0.004266098  0.6274510 0.006799093 2.632276    32
## [4]  {frozen vegetables,                                                                      
##       soup}               => {mineral water} 0.005065991  0.6333333 0.007998933 2.656954    38
## [5]  {ground beef,                                                                            
##       soup}               => {mineral water} 0.005065991  0.5205479 0.009732036 2.183798    38
## [6]  {milk,                                                                                   
##       soup}               => {mineral water} 0.008532196  0.5614035 0.015197974 2.355194    64
## [7]  {chocolate,                                                                              
##       soup}               => {mineral water} 0.005599253  0.5526316 0.010131982 2.318395    42
## [8]  {eggs,                                                                                   
##       soup}               => {mineral water} 0.004932676  0.5441176 0.009065458 2.282677    37
## [9]  {soup,                                                                                   
##       spaghetti}          => {mineral water} 0.007465671  0.5233645 0.014264765 2.195614    56
## [10] {cooking oil,                                                                            
##       pancakes}           => {mineral water} 0.004666045  0.5932203 0.007865618 2.488672    35
## [11] {cooking oil,                                                                            
##       ground beef}        => {spaghetti}     0.004799360  0.5714286 0.008398880 3.281995    36
## [12] {cooking oil,                                                                            
##       eggs}               => {mineral water} 0.006399147  0.5454545 0.011731769 2.288286    48
## [13] {pancakes,                                                                               
##       whole wheat rice}   => {mineral water} 0.004132782  0.5961538 0.006932409 2.500979    31
## [14] {frozen vegetables,                                                                      
##       turkey}             => {mineral water} 0.004399413  0.5000000 0.008798827 2.097595    33
## [15] {milk,                                                                                   
##       turkey}             => {mineral water} 0.006132516  0.5411765 0.011331822 2.270338    46
## [16] {chicken,                                                                                
##       pancakes}           => {mineral water} 0.004799360  0.5294118 0.009065458 2.220983    36
## [17] {chicken,                                                                                
##       ground beef}        => {spaghetti}     0.004799360  0.5070423 0.009465405 2.912193    36
## [18] {chicken,                                                                                
##       chocolate}          => {mineral water} 0.007598987  0.5181818 0.014664711 2.173871    57
## [19] {ground beef,                                                                            
##       low fat yogurt}     => {mineral water} 0.004799360  0.5000000 0.009598720 2.097595    36
## [20] {olive oil,                                                                              
##       tomatoes}           => {spaghetti}     0.004399413  0.6111111 0.007199040 3.509912    33
## [21] {olive oil,                                                                              
##       tomatoes}           => {mineral water} 0.004132782  0.5740741 0.007199040 2.408350    31
## [22] {olive oil,                                                                              
##       shrimp}             => {mineral water} 0.004532729  0.5573770 0.008132249 2.338303    34
## [23] {frozen vegetables,                                                                      
##       olive oil}          => {spaghetti}     0.005732569  0.5058824 0.011331822 2.905531    43
## [24] {frozen vegetables,                                                                      
##       olive oil}          => {mineral water} 0.006532462  0.5764706 0.011331822 2.418404    49
## [25] {milk,                                                                                   
##       olive oil}          => {mineral water} 0.008532196  0.5000000 0.017064391 2.097595    64
## [26] {chocolate,                                                                              
##       olive oil}          => {mineral water} 0.008265565  0.5040650 0.016397814 2.114649    62
## [27] {ground beef,                                                                            
##       shrimp}             => {spaghetti}     0.005999200  0.5232558 0.011465138 3.005315    45
## [28] {ground beef,                                                                            
##       pancakes}           => {mineral water} 0.007465671  0.5137615 0.014531396 2.155327    56
## [29] {frozen vegetables,                                                                      
##       ground beef}        => {spaghetti}     0.008665511  0.5118110 0.016931076 2.939582    65
## [30] {frozen vegetables,                                                                      
##       ground beef}        => {mineral water} 0.009198773  0.5433071 0.016931076 2.279277    69
## [31] {ground beef,                                                                            
##       milk}               => {mineral water} 0.011065191  0.5030303 0.021997067 2.110308    83
## [32] {eggs,                                                                                   
##       ground beef}        => {mineral water} 0.010131982  0.5066667 0.019997334 2.125563    76
## [33] {frozen vegetables,                                                                      
##       ground beef,                                                                            
##       spaghetti}          => {mineral water} 0.004399413  0.5076923 0.008665511 2.129866    33
## [34] {frozen vegetables,                                                                      
##       milk,                                                                                   
##       spaghetti}          => {mineral water} 0.004532729  0.5483871 0.008265565 2.300588    34
## [35] {chocolate,                                                                              
##       frozen vegetables,                                                                      
##       spaghetti}          => {mineral water} 0.004132782  0.5254237 0.007865618 2.204252    31
plot(subrules)

top10rules<-head(subrules,n=10,by="confidence")
plot(top10rules,method="graph")

plot(rules)

plot(rules,measure="confidence")
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.

plot(rules,method="two-key plot")

plot(rules,engine="plotly")
subrules<-head(rules,n=10, by="confidence")

plot(subrules,method="graph",engine="htmlwidget")
plot(subrules,method="paracoord")